﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FileOPeration
{
     public class FileController
    {
        public static string dirPath = "D:\\Git_resposity\\learning_at_working\\C#\\FileProgramSolution\\FileOPeration\\file\\";

        public static string fileName1 = "test001.txt";
        public static string fileName2 = "test002.txt";
        public static string fileName3 = "test003.txt";

        public static void Delete(string fileName)
        {

            DirectoryInfo dirRoot = new DirectoryInfo(dirPath);
            if (!dirRoot.Exists)
            {
                Console.WriteLine("The specified directory does not exist.");
                return;
            }
            FileInfo[] files = dirRoot.GetFiles();

            foreach (FileInfo file in files)
            {
                if (file.Name == fileName)
                {
                    System.IO.File.Delete(file.FullName);
                    Console.WriteLine("删除" + fileName + "成功");
                    break ;
                }
            }
            Console.WriteLine(dirPath + "目录下files文件个数" + files.Length);
            Console.WriteLine("删除后个数" + dirRoot.GetFiles().Length);

        }

        public static void createFile(string fileName1,string fileName2)
        {
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
            Console.WriteLine(dirPath + "目录创建成功！");
            Console.WriteLine("在目录" + dirPath + "下开始创建文件。。。。。。");
            using (FileStream fs = File.Create(Path.Combine(dirPath, fileName1)))
            {
                fs.Close();
                Console.WriteLine($"File {fileName1} created successfully in {dirPath}");
            }
            using (FileStream fs1 = File.Create(Path.Combine(dirPath, fileName2)))
            {
                fs1.Close();
                Console.WriteLine($"File {fileName2} created successfully in {dirPath}");
            }
            DirectoryInfo dirRoot = new DirectoryInfo(dirPath);
            Console.WriteLine("创建后个数" + dirRoot.GetFiles().Length);
        }

        public static void readFile(string fileName)
        {
            string filePath = dirPath + fileName;
            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    string line;
                    Console.WriteLine("File content line by line using StreamReader:");
                    while ((line = reader.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        Console.WriteLine("执行");
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }

       
    }
}
